package com.bishe.tax.controller.sys;

import java.util.List;

import javax.servlet.http.HttpSession;

import com.bishe.tax.entity.ChangeTaxRegistration;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.bishe.tax.entity.SysUser;
import com.bishe.tax.entity.TaxRegistration;
import com.bishe.tax.entity.Taxpayer;
import com.bishe.tax.service.TaxRegistrationService;
import com.bishe.tax.service.TaxpayerService;
import com.bishe.tax.service.ToDoaskService;
import com.bishe.tax.utils.AjaxResult;
import com.bishe.tax.utils.TableDataInfo;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
 * 税务登记 信息操作处理
 *
 * @author ruoyi
 * @date 2019-06-23
 */
@Controller
@RequestMapping("/system/registration")
public class TaxRegistrationController {
	private String prefix = "taxRegistration";

	@Autowired
	private TaxRegistrationService registrationService;
	@Autowired
	private TaxpayerService taxpayerService;
	@Autowired
	private ToDoaskService toDoaskService;

	@GetMapping("list")
	public String registration(@RequestParam(value = "type", required = false) String type, ModelMap mmap) {
		mmap.put("type", type);
		return prefix + "/list";
	}

	@GetMapping("infoList")
	public String infoList() {
		return prefix + "/trList";
	}

	@GetMapping("deleteList")
	public String deleteList() {
		return prefix + "/deleteList";
	}

	/**
	 * 查询税务登记列表
	 */
	@PostMapping("/list")
	@ResponseBody
	public TableDataInfo list(TaxRegistration registration,
			@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
			@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
		PageHelper.startPage(pageNum, pageSize);
		List<TaxRegistration> list = registrationService.selectRegistrationList(registration);
		return TableDataInfo.returnDataTable(new PageInfo<>(list).getTotal(), list);
	}

	/**
	 * 查询纳税人列表
	 */
	@PostMapping("/trlist")
	@ResponseBody
	public TableDataInfo trlist(TaxRegistration registration,
			@RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
			@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum) {
		PageHelper.startPage(pageNum, pageSize);
		List<TaxRegistration> list = registrationService.selectRegistrationListByChoosen(registration);
		return TableDataInfo.returnDataTable(new PageInfo<>(list).getTotal(), list);
	}

	/**
	 * 新增税务登记
	 */
	@GetMapping("/add")
	public String add(@RequestParam("taxpayerId") String taxpayerId, ModelMap mmap) {
		Taxpayer taxpayer = taxpayerService.selectTaxpayerById(Integer.valueOf(taxpayerId));
		mmap.put("taxpayer", taxpayer);
		return prefix + "/add";
	}

	/**
	 * 新增保存税务登记
	 */
	@PostMapping("/add")
	@ResponseBody
	public AjaxResult addSave(TaxRegistration registration, @RequestParam("taxpayerId") String taxpayerId) {
		registrationService.insertRegistration(registration);
		Taxpayer taxpayer = taxpayerService.selectTaxpayerById(Integer.valueOf(taxpayerId));
		taxpayer.setTxId(registration.getTxId());
		taxpayerService.updateTaxpayer(taxpayer);
		return AjaxResult.success();
	}

	/**
	 * 查看税务登记
	 */
	@GetMapping("/show/{txId}")
	public String show(@PathVariable("txId") Integer txId,
					   @RequestParam(value = "taskId", required = false) String taskId, ModelMap mmap) {
		TaxRegistration registration = registrationService.selectRegistrationById(txId);
		mmap.put("taxRegistration", registration);
		mmap.put("taskId", taskId);
		return prefix + "/show";
	}


	/**
	 * 修改税务登记
	 */
	@GetMapping("/edit/{txId}")
	public String edit(@PathVariable("txId") Integer txId,
			@RequestParam(value = "taskId", required = false) String taskId, ModelMap mmap) {
		TaxRegistration registration = registrationService.selectRegistrationById(txId);
		mmap.put("taxRegistration", registration);
		mmap.put("taskId", taskId);
		return prefix + "/edit";
	}

	/**
	 * 修改保存税务登记
	 */
	@PostMapping("/edit")
	@ResponseBody
	public AjaxResult editSave(TaxRegistration registration,
			@RequestParam(value = "taskId", required = false) String taskId) {
		if (StringUtils.isNotEmpty(taskId)) {
			toDoaskService.doComplete(taskId, null);
			registration.setStatus(2);
		}
		return AjaxResult.success(registrationService.updateRegistration(registration));
	}

	/**
	 * 删除税务登记
	 */
	@PostMapping("/remove")
	@ResponseBody
	public AjaxResult remove(String ids) {
		return AjaxResult.success(registrationService.deleteRegistrationByIds(ids));
	}

	/**
	 * 删除税务登记
	 */
	@PostMapping("/delete")
	@ResponseBody
	public AjaxResult delete(String ids, HttpSession session) {
		SysUser user = (SysUser) session.getAttribute("sysUser");
		TaxRegistration registration = registrationService.selectRegistrationById(Integer.valueOf(ids));
		return AjaxResult.success(registrationService.doSubmitDeleteFlow(user, registration));
	}

	/**
	 * 审核页面
	 *
	 * @param taskId
	 * @param flag
	 */
	@GetMapping("examine/{taskId}/{txId}")
	public String doExamine(@PathVariable("txId") String txId, @PathVariable("taskId") String taskId,
			@RequestParam(value = "type", required = false) String type, ModelMap mmap) {
		TaxRegistration registration = registrationService.selectRegistrationById(Integer.valueOf(txId));
		mmap.put("taxRegistration", registration);
		mmap.put("taskId", taskId);
		mmap.put("type", type);
		return prefix + "/taxRegistration";
	}

	/**
	 * 审核
	 *
	 * @param taskId
	 * @param flag
	 */
	@PostMapping("complete")
	@ResponseBody
	public AjaxResult doComplete(TaxRegistration taxRegistration, @RequestParam("taskId") String taskId,
			@RequestParam(value = "type", required = false) String type, @RequestParam("flag") Boolean flag) {
		toDoaskService.doComplete(taskId, flag);
		if (StringUtils.isNotBlank(type)) {
			if (flag) {
				taxRegistration.setStatus(0);
			} else {
				taxRegistration.setStatus(4);
			}
		} else {
			if (flag) {
				taxRegistration.setStatus(4);
			} else {
				taxRegistration.setStatus(3);
			}
		}

		return AjaxResult.success(registrationService.updateRegistration(taxRegistration));
	}

	/**
	 * 提交审核
	 *
	 * @return
	 */
	@PostMapping("submitFlow")
	@ResponseBody
	public AjaxResult submitFlow(@RequestParam("txId") String txId, HttpSession session) {
		SysUser user = (SysUser) session.getAttribute("sysUser");
		return AjaxResult.success(registrationService.doSubmitFlow(user, txId));
	}

	/**
	 * 变更税务登记
	 */
	@GetMapping("/change/{txId}")
	public String change(@PathVariable("txId") Integer txId,
			@RequestParam(value = "taskId", required = false) String taskId, ModelMap mmap) {
		TaxRegistration registration = registrationService.selectRegistrationById(txId);
		mmap.put("taxRegistration", registration);
		mmap.put("taskId", taskId);
		return prefix + "/change";
	}

	/**
	 * 保存变更税务登记
	 */
	@PostMapping("/change")
	@ResponseBody
	public AjaxResult changeSave(TaxRegistration registration, HttpSession session) {
		SysUser user = (SysUser) session.getAttribute("sysUser");
		return AjaxResult.success(registrationService.doSubmitChangeFlow(user, registration));
	}

	/**
	 * 审核
	 *
	 * @param taskId
	 * @param flag
	 */
	@PostMapping("completeChange")
	@ResponseBody
	public AjaxResult doCompleteChange(TaxRegistration taxRegistration, @RequestParam("taskId") String taskId,
			@RequestParam("flag") Boolean flag) {
		toDoaskService.doComplete(taskId, flag);
		return registrationService.updateChange(taxRegistration,flag);
	}

	/**
	 * 审核页面
	 *
	 * @param taskId
	 * @param flag
	 */
	@GetMapping("examineChange/{taskId}/{txId}")
	public String doExamineChange(@PathVariable("txId") String txId, @PathVariable("taskId") String taskId,
			ModelMap mmap) {
		ChangeTaxRegistration registration = registrationService.selectChangeRegistrationById(Integer.valueOf(txId));
		mmap.put("taxRegistration", registration);
		mmap.put("taskId", taskId);
		return prefix + "/taxRegistrationChange";
	}

	/**
	 * 修改保存税务登记
	 */
	@PostMapping("/editChange")
	@ResponseBody
	public AjaxResult editChange(TaxRegistration registration,
			@RequestParam(value = "taskId", required = false) String taskId) {
		if (StringUtils.isNotEmpty(taskId)) {
			toDoaskService.doComplete(taskId, null);
			registration.setStatus(5);
		}
		return AjaxResult.success(registrationService.updateRegistration(registration));
	}
}
